package org.mklink.service.impl;

import cn.hutool.core.util.RuntimeUtil;
import org.mklink.domain.MkLink;
import org.mklink.enums.ControlEnum;
import org.mklink.util.AlertUtil;
import org.mklink.util.FileUtil;
import org.mklink.util.LanguageUtil;
import org.mklink.util.MultiThreadFileMover;
import org.mklink.util.UI;

import java.io.File;
import java.util.Objects;

/**
 * /J 为目录创建联接点，属于软链接，只能是绝对路径的链接。
 * @Author: wyx
 * @Date: 2023/8/20 12:35
 */
public class JMkLinkServiceImpl extends MkLinkService {
    
    @Override
    public boolean doMkLink(MkLink link){
        //判断是否存在软链接
        MultiThreadFileMover mover = new MultiThreadFileMover();
        mover.moveDirectory(link.getSourcePath(), link.getTargetPath(), link.getThreadCount(), () -> {
            UI.updateLabelText(ControlEnum.BTN_LINK, LanguageUtil.getString("button.link"));
            UI.setDisable(false);
            //判断源路径下是否还有文件
            if (Objects.requireNonNull(new File(link.getSourcePath()).listFiles()).length>0) {
                UI.addListViewItem(ControlEnum.LIST_VIEW_ERROR, LanguageUtil.getString("error.check.log"));
                AlertUtil.information(LanguageUtil.getString("error.check.log"));
                return;
            }
            //删除源文件夹
            if(!FileUtil.del(link.getSourcePath())){
                UI.addListViewItem(ControlEnum.LIST_VIEW_ERROR, LanguageUtil.getString("error.delete.failed")+link.getSourcePath());
                AlertUtil.information(LanguageUtil.getString("error.delete.failed")+link.getSourcePath());
                return;
            }
            //组装cmd命令
            String cmd = getCmdByMode(link.getSourcePath(),link.getTargetPath(),link.getMode());
            //获取执行的返回结果
            String cmdRet = RuntimeUtil.execForStr(cmd);
            //判断是否链接成功
            boolean ret = cmdRet.contains("<<===>>");
            if (ret) {
                UI.addListViewItem(ControlEnum.LIST_VIEW_SUCCESS, LanguageUtil.getString("success.link") + "\n" + cmdRet);
                AlertUtil.information(LanguageUtil.getString("success.link") + "\n" + cmdRet);
            } else {
                UI.addListViewItem(ControlEnum.LIST_VIEW_ERROR, LanguageUtil.getString("error.link.failed") + "\n" + cmdRet);
                AlertUtil.information(LanguageUtil.getString("error.link.failed") + "\n" + cmdRet);
            }
        });
        return true;
    }
    
    /**
     * 解除软链接
     * @param link
     * @return
     */
    @Override
    protected boolean doUnMkLink(MkLink link) {
        try {
            // 使用fsutil命令删除软链接
            String cmd = "cmd /c fsutil reparsepoint delete \"" + link.getSourcePath() + "\"";
            String result = RuntimeUtil.execForStr(cmd);
            
            // 检查是否执行成功（fsutil成功时通常返回空字符串或成功信息）
            boolean success = result == null || result.trim().isEmpty() || !result.toLowerCase().contains("error");
            
            if (success) {
                UI.setDisable(false);
                UI.updateLabelText(ControlEnum.BTN_UNLINK, LanguageUtil.getString("button.unlink"));
                UI.addListViewItem(ControlEnum.LIST_VIEW_SUCCESS, LanguageUtil.getString("success.unlink"));
                AlertUtil.information(LanguageUtil.getString("success.unlink"));
                return true;
            } else {
                UI.setDisable(false);
                UI.updateLabelText(ControlEnum.BTN_UNLINK, LanguageUtil.getString("button.unlink"));
                UI.addListViewItem(ControlEnum.LIST_VIEW_ERROR, LanguageUtil.getString("error.unlink.failed") + result);
                AlertUtil.information(LanguageUtil.getString("error.unlink.failed") + result);
                return false;
            }
        } catch (Exception e) {
            UI.setDisable(false);
            UI.updateLabelText(ControlEnum.BTN_UNLINK, LanguageUtil.getString("button.unlink"));
            UI.updateLabelText(ControlEnum.LIST_VIEW_ERROR, LanguageUtil.getString("error.unlink.exception") + e.getMessage());
            AlertUtil.information(LanguageUtil.getString("error.unlink.exception") + e.getMessage());
            return false;
        }
    }
}
